<?php $this->beginBlock('title');?>
添加文章
<?php $this->endBlock();?>
<?php $this->params['breadcrumbs'] =
    [
        [
            'label' => '添加文章',
//            'url' => \yii\helpers\Url::to(['admin/article/add']),
            'class' => 'active',
        ]
    ];
?>
<?php $this->beginBlock('headerCssFile')?>

<?php $this->endBlock()?>

<?php $this->beginBlock('headerCssText')?>
    <style>

    </style>
<?php $this->endBlock()?>

<div class="row">
    <div class="col-md-12">

        <div class="block-flat">
            <div class="header">
                <h4>添加文章</h4>
            </div>
            <div class="content">
                <form class="form-horizontal group-border-dashed" id="articleData">
                    <input type="hidden" name="id" value="<?php echo !empty($article['id']) ? $article['id'] : ''?>">
                    <input name="<?= Yii::$app->request->csrfParam;?>" type="hidden" value="<?= Yii::$app->request->csrfToken ?>">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">文章标题</label>
                        <div class="col-sm-9">
                            <input type="text" name="title" value="<?php echo !empty($article['title']) ? $article['title'] : ''?>" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">所属分类</label>
                        <div class="col-sm-9">
                            <select class="form-control" name="category_id">
                                <option value="0">请选择分类</option>
                                <?php if (!empty($cate)):?>
                                    <?php foreach ($cate as $value):?>
                                        <option value="<?=!empty($value['id']) ? $value['id'] : 0?>"><?=$value['html'].$value['name']?></option>
                                    <?php endforeach;?>
                                <?php endif ;?>
                            </select>

<!--                            <input type="text" value="--><?php //echo !empty($pInfo['pname']) ? $pInfo['pname'] : '根目录';?><!--" class="form-control" readonly="true">-->
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">文章描述</label>
                        <div class="col-sm-9">
                            <textarea name="desc" class="form-control"><?php echo !empty($article['desc']) ? $article['desc'] : ''?></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="padding-bottom: 0px">
                        <label class="col-sm-2 control-label">封面图</label>
                        <div class="col-sm-10">
                            <button type="button" id="upload" class="btn btn-primary"><i class="fa fa-cloud-upload"></i> 点击上传</button>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <img id="thumb" src="/images/upload.png" class="img-thumbnail" style="height:128px;">
                            <input type="hidden" name="thumb" value="<?php echo !empty($article['thumb']) ? $article['thumb'] : ''?>">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">文章内容</label>
                        <div class="col-sm-9">
                            <textarea class="layui-textarea" id="editor" style="display: none" name="content">请输入文章内容</textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="button" onclick="saveInfo.saveData()" class="btn btn-primary"><i class="fa fa-save"></i>保存</button>
                            <button onclick="history.go(-1);" class="btn btn-default"><i class="fa fa-mail-reply"></i>返回</button>
                        </div>
                    </div>

                </form>
            </div>
        </div>

    </div>
</div>
<?php $this->beginBlock('footerJsFile')?>
    <script src="/plugins/layui/layui.js"></script>
<?php $this->endBlock()?>

<?php $this->beginBlock('footerJsText')?>

<script type="text/javascript">
    var layedit,layerIndex;
    // 编辑器相关操作
    layui.use('layedit', function(){
        layedit = layui.layedit
            ,$ = layui.jquery;
        // 初始化图片上传
        layedit.set({
            uploadImage: {
                url: '<?php echo \yii\helpers\Url::to(['admin/api/upload/content'])?>' //接口url
                ,type: 'post' //默认post
                ,data:{
                    '<?php echo Yii::$app->request->csrfParam?>':'<?php echo Yii::$app->request->csrfToken?>',
                    category:'image',
                    action:'about'

                }//额外数据
            }
        });

        //构建一个默认的编辑器
        layerIndex = layedit.build('editor');
    });

    // 上传封面图相关操作
    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            ,url: '<?php echo \yii\helpers\Url::to(['admin/api/upload/index'])?>' //上传接口
            ,method:'post'//请求方式
            ,data:{
                '<?php echo Yii::$app->request->csrfParam?>':'<?php echo Yii::$app->request->csrfToken?>',
                category:'image',
                action:'thumb'

            }//额外数据
            ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                layer.load(); //上传loading
                obj.preview(function(index, file, result){
                    $('#thumb').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //上传完毕回调
                $('input[name=thumb]').val(res.path);
                layer.closeAll('loading'); //关闭loading
            }
            ,error: function(){
                //请求异常回调
                layer.closeAll('loading'); //关闭loading
            }
        });
    });

    var saveInfo = {
        saveData:function () {
            layedit.sync(layerIndex);
            var data = $("#articleData").serialize();
            ajaxLock = false;
            if (ajaxLock)return false;
            ajaxLock = true;
            $.ajax({
                type:'POST',
                dataType:'json',
                data:data,
                url:'<?php echo \yii\helpers\Url::to(['admin/article/save'])?>',
                success:function (resp) {
                    layer.msg(resp.m);
                },
                error:function () {
                    console.log("save失败");
                }
            });
        }
    };
</script>
<?php $this->endBlock()?>